package leetcode.listnode;

import com.alibaba.fastjson.support.odps.udf.CodecCheck;

import java.util.*;

public class ReverseBetween {

    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (left != right) {
            List<Integer> list = new ArrayList<>();
            ListNode node = head;
            ListNode currentHeadNode = null;
            ListNode currentTailNode = null;
            int index = 1;
            while (node != null) {
                if (index == left) {
                    currentHeadNode = node;
                } else if (index == right) {
                    currentTailNode = node;
                    Integer temp = currentHeadNode.val;
                    currentHeadNode.val = currentTailNode.val;
                    currentTailNode.val = temp;

                    for (int i = list.size() - 1; i >= 0; i--) {
                        currentHeadNode.next.val = list.get(i);
                        currentHeadNode = currentHeadNode.next;
                    }

                } else if (index > left && index < right) {
                    list.add(node.val);
                }
                index++;
                node = node.next;
            }
        }
        return head;
    }

}
